<template>
	<el-select v-model="selectValue" v-bind="$attrs">
		<el-option v-for="(item, index) in optionArrs" :key="index" :label="item.label" :value="item.value"></el-option>
	</el-select>
</template>

<script>
import emitter from 'element-ui/src/mixins/emitter';
export default {
	name: 'swCustomSelect',
	inject: ['elForm'],
	mixins: [emitter],
	props: {
		value: [String, Array],
		options: { type: Array, default: () => [] },
		props: {type: Object,
			default() {
				return { label: 'name', value: 'id' };
			}
		}
	},
	data() {
		return {
			selectValue: this.value
		};
	},
	computed: {
		optionArrs() {
			return this.options.map((item) => {
				return {
					label: item[this.props.label],
					value: item[this.props.value]
				};
			});
		}
	},
	watch: {
		value(value) {
			this.selectValue = value;
		},
		selectValue(value) {
			this.$emit('input', value);
			this.dispatch('ElFormItem', 'el.form.change', [value]);
		}
	}
};
</script>
